//
//  TabbarViewController.m
//  WBBaseProject
//
//  Created by 吴斌 on 2018/3/2.
//  Copyright © 2018年 吴斌. All rights reserved.
//

#import "TabbarViewController.h"
#import "TabBar.h"

#import "HomeViewController.h"
#import "InvestViewController.h"
#import "MessageViewController.h"
#import "MyViewController.h"
#import "PublicBorrowViewController.h"

#import "WalletViewController.h"
#import "AddDepositCardViewController.h"
#import "AddBankCardViewController.h"

#import "KLTNavigationController.h"

@interface TabbarViewController ()

@property (nonatomic, strong) UINavigationController *rootNav;

@end

@implementation TabbarViewController

#pragma mark - 生命周期 Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    HomeViewController *firstVC = [[HomeViewController alloc] init];
    [self addChildController:firstVC title:@"首页" imageName:@"tabbar_home" selectedImageName:@"tabbar_home_selected" navVc:[KLTNavigationController class]];
    
    InvestViewController *secondVC = [[InvestViewController alloc] init];
    [self addChildController:secondVC title:@"投资" imageName:@"tabbar_message_center" selectedImageName:@"tabbar_message_center_selected" navVc:[KLTNavigationController class]];
    
    MessageViewController *thirdVC = [[MessageViewController alloc] init];    
    [self addChildController:thirdVC title:@"消息" imageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_selected" navVc:[KLTNavigationController class]];
    
    MyViewController *fourVC = [[MyViewController alloc] init];
    [self addChildController:fourVC title:@"我的" imageName:@"tabbar_profile" selectedImageName:@"tabbar_profile_selected" navVc:[KLTNavigationController class]];
    
    [[UITabBar appearance] setBackgroundImage:[self imageWithColor:WB_RGBColor(246, 246, 246)]];
    //设置tabbar
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
    //设置自定义的tabbar
    [self setCustomtabbar];
}

- (void)setCustomtabbar
{
    TabBar *tabbar = [[TabBar alloc] init];
    [self setValue:tabbar forKey:@"tabBar"];
    [tabbar.centerBtn addTarget:self action:@selector(centerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)centerBtnClick:(UIButton *)btn
{
//    if (![Factory isLogin]) {
//        [Factory loadLoginScreen];
//    }else{
//        //如果额度已激活进入选择银行卡页面
//        AddBankCardViewController *vc = [[AddBankCardViewController alloc] init];
//        vc.bankType = AddBankChoose;
//        vc.hidesBottomBarWhenPushed = YES;
//        id delegate = [UIApplication sharedApplication].delegate;
//        UITabBarController *tabbarController = [delegate tabBarController];
//        [tabbarController.selectedViewController pushViewController:vc animated:YES];
//    }
    AddBankCardViewController *vc = [[AddBankCardViewController alloc] init];
    vc.bankType = AddBankChoose;
    vc.hidesBottomBarWhenPushed = YES;
    id delegate = [UIApplication sharedApplication].delegate;
    UITabBarController *tabbarController = [delegate tabBarController];
    [tabbarController.selectedViewController pushViewController:vc animated:YES];
}

- (void)addChildController:(UIViewController*)childController title:(NSString*)title imageName:(NSString*)imageName selectedImageName:(NSString*)selectedImageName navVc:(Class)navVc{
    childController.title = title;
    childController.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    childController.tabBarItem.badgeValue = @"66";
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //设置一下选中tabbar文字颜色
    [childController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : WB_RGBColor(105, 142, 227)} forState:UIControlStateSelected];
    
    KLTNavigationController *nav = [[navVc alloc] initWithRootViewController:childController];
    _rootNav = nav;
    [self addChildViewController:nav];
}

- (UIImage *)imageWithColor:(UIColor *)color{
    // 一个像素
    CGRect rect = CGRectMake(0, 0, 1, 1);
    // 开启上下文
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    [color setFill];
    UIRectFill(rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end
